package com.thenie.chat.util;

import cn.hutool.json.JSONUtil;
import com.thenie.chat.pojo.User;
import com.thenie.chat.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.List;

/**
 * @Description
 * @Author thenie
 * @Data 2022/5/28 19:16
 */
@Slf4j
public class WebSocketUtil {
    public static final String CURRENT_USER_ID = "currentUserId";

    public static final String CURRENT_USER = "currentUser";

    /**
     * 返回用户 Id
     *
     * @param session
     * @return
     */
    public static String getUserId(WebSocketSession session) {
        return (String) session.getAttributes().get(CURRENT_USER_ID);
    }

    /**
     * 返回用户 详细信息
     *
     * @param session
     * @return
     */
    public static User getCurrentUser(WebSocketSession session) {
        Object user = session.getAttributes().get(CURRENT_USER);
        User res = new User();
        BeanUtils.copyProperties(user, res);
        return res;
    }

    /**
     * 发送消息
     *
     * @param session
     * @param message
     * @throws IOException
     */
    public static void sendMessage(WebSocketSession session, DialogMessage message) {
        try {
            RespBean obj = RespBean.success(RespBeanEnum.SEND_ONE_MESSAGE, message);//1
            session.sendMessage(new TextMessage(JSONUtil.toJsonStr(obj)));
        } catch (IOException e) {
            log.warn("WebSocketUtil.sendMessage", e);
        }
    }

    /**
     * 发送离线消息（用于上线接收
     *
     * @param session
     * @param messageList
     * @throws IOException
     */
    public static void sendOfflineMessage(WebSocketSession session, List<Object> messageList) {
        try {
            RespBean obj = RespBean.success(RespBeanEnum.SEND_OFFLINE_MESSAGE, messageList);//2
            session.sendMessage(new TextMessage(JSONUtil.toJsonStr(obj)));
        } catch (IOException e) {
            log.warn("WebSocketUtil.sendOfflineMessage", e);
        }
    }

    /**
     * 发送好友请求
     *
     * @param session
     * @param friendRequestVo
     * @throws IOException
     */
    public static void sendFriendRequest(WebSocketSession session, FriendRequestVo friendRequestVo) {
        try {
            RespBean obj = RespBean.success(RespBeanEnum.SEND_FRIEND_REQUEST_MESSAGE, friendRequestVo);//3
            session.sendMessage(new TextMessage(JSONUtil.toJsonStr(obj)));
        } catch (IOException e) {
            log.warn("WebSocketUtil.sendFriendRequest", e);
        }
    }

    /**
     * 有新的好友验证通过
     *
     * @param session
     * @param user
     * @throws IOException
     */
    public static void sendNewFriendMsg(WebSocketSession session, UserInfoVo user) {
        try {
            RespBean obj = RespBean.success(RespBeanEnum.SEND_NEW_FRIEND_MESSAGE, user);//4
            session.sendMessage(new TextMessage(JSONUtil.toJsonStr(obj)));
        } catch (IOException e) {
            log.warn("WebSocketUtil.sendNewFriendMsg", e);
        }
    }

    /**
     * 通知好友上线
     *
     * @param session
     * @param id
     * @throws IOException
     */
    public static void sendOnlineUpMsg(WebSocketSession session, Long id) {
        try {
            RespBean obj = RespBean.success(RespBeanEnum.SEND_ONLINE_UP_MESSAGE, id);//5
            session.sendMessage(new TextMessage(JSONUtil.toJsonStr(obj)));
        } catch (IOException e) {
            log.warn("WebSocketUtil.sendOnlineUpMsg", e);
        }
    }

    /**
     * 通知好友下线
     *
     * @param session
     * @param id
     * @throws IOException
     */
    public static void sendOnlineDownMsg(WebSocketSession session, Long id) {
        try {
            RespBean obj = RespBean.success(RespBeanEnum.SEND_ONLINE_DOWN_MESSAGE, id);//6
            session.sendMessage(new TextMessage(JSONUtil.toJsonStr(obj)));
        } catch (IOException e) {
            log.warn("WebSocketUtil.sendOnlineDownMsg", e);
        }
    }


    /**
     * 发送随机匹配成功通知
     * @param session
     * @param uid
     */
    public static void sendRandomChatOkMsg(WebSocketSession session, String uid) {
        try {
            RespBean obj = RespBean.success(RespBeanEnum.SEND_RANDOM_CHAT_OK_MESSAGE, uid);//7
            session.sendMessage(new TextMessage(JSONUtil.toJsonStr(obj)));
        } catch (IOException e) {
            log.warn("WebSocketUtil.sendRandomChatOkMsg", e);
        }
    }

}
